package com.tmans.adminController;

import java.lang.reflect.Member;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.tmans.adminDao.AdminDto;
import com.tmans.adminDao.AdminService;
import com.tmans.common.PageData;
import com.tmans.common.util.WebUtil;


/**
 * Handles requests for the application home page.
 */
@Controller
public class AdminController {
	
	@Autowired
	private AdminService adminSerVice;
	
	private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
	
	
	
	/**
	 * page move
	 */
	@RequestMapping(value = "/admin/form", method = RequestMethod.GET)
	public void form(Locale locale, Model model) {

		
	}
	
	


	/*
	 * 以�났 泥댄� (怨��踰��)
	 */
	@RequestMapping(value = "/admin/board/dupcheck", method = RequestMethod.POST)
    public  ModelAndView dupchek(Model model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
		
	  ModelAndView mav = new ModelAndView();
	  mav.setViewName("jsonView");
	
		
		try {
			
				int value =0;
				
				value =adminSerVice.getDupCheck(adminDto.getGroupid());
				
				  
			  if(value ==0){ //占싻쏙옙占쏙옙占쏙옙 체크占쏙옙 占쏙옙占쏙옙占싹몌옙  
				   mav.addObject("result", "0");
				   mav.addObject("msg", "占쌩븝옙 체크  占쏙옙占쏙옙");
			  }else if(value ==1){
			   
				   mav.addObject("result", "1");
				   mav.addObject("msg", "占쌩븝옙 占쏙옙占쏙옙 占쏙옙호占쏙옙 占쌍쏙옙占싹댐옙. 占쌕쏙옙 占쌉뤄옙占쏙옙占쌍쇽옙占쏙옙");
			  }else{
				   mav.addObject("result", "-1");
				   mav.addObject("msg", "占쏙옙트占쏙옙크 占쏙옙占�");
			  }
				
				
		} catch (Exception e) {
			// TODO: handle exception
						
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	/*
	 *  리스트
	 */ 
	@RequestMapping(value = "/admin/iplist", method = RequestMethod.GET)
	public  ModelAndView iplist(ModelMap model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
		
		ModelAndView  mv = new ModelAndView();
		try {
			
			
			PageData pd = new PageData();
			
			if(adminDto.getCpage()==null || "".equals(adminDto.getCpage())){
				
				pd.setCpage(1);			 //��㈃ ���		
				adminDto.setIntcpage(0); //荑쇰━ ���  
				
			}else{
				
				pd.setCpage(Integer.parseInt(adminDto.getCpage()));
				adminDto.setIntcpage(Integer.parseInt(adminDto.getCpage()) -1);
			}
			
			
			
			
			int index = adminDto.getIntcpage();
			if(index==0){}
			else{index*=10;}
			 
			adminDto.setIntcpage(index);
			//test

			//total
			adminDto.setMax(adminSerVice.getIpListCnt(adminDto));
			
			
			pd.setTotalPage((adminDto.getMax() / 10)+1);			
			pd.setEndPage(pd.getTotalPage());
			pd.setNextPage();
			pd.setPrevPage();
			
			
			
			mv.addObject("list",adminSerVice.getIpList(adminDto));
			mv.addObject("pd",pd);
			mv.addObject("adminDto",adminDto);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
		return mv;
	}
	
	/*
	 *  리스트
	 */ 
	@RequestMapping(value = "/admin/pcbanglist", method = RequestMethod.GET)
	public  ModelAndView artBoard(ModelMap model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
		
		ModelAndView  mv = new ModelAndView();
		try {
			
			
			PageData pd = new PageData();
			
			if(adminDto.getCpage()==null || "".equals(adminDto.getCpage())){
				
				pd.setCpage(1);			 //��㈃ ���		
				adminDto.setIntcpage(0); //荑쇰━ ���  
				
			}else{
				
				pd.setCpage(Integer.parseInt(adminDto.getCpage()));
				adminDto.setIntcpage(Integer.parseInt(adminDto.getCpage()) -1);
			}
			
			
			
			
			int index = adminDto.getIntcpage();
			if(index==0){}
			else{index*=10;}
			 
			adminDto.setIntcpage(index);
			//test

			//total
			adminDto.setMax(adminSerVice.getPcBangCnt(adminDto));
			
			
			pd.setTotalPage((adminDto.getMax() / 10)+1);			
			pd.setEndPage(pd.getTotalPage());
			pd.setNextPage();
			pd.setPrevPage();
			
			
			
			mv.addObject("list",adminSerVice.getPcBangList(adminDto));
			mv.addObject("pd",pd);
			mv.addObject("adminDto",adminDto);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
		return mv;
	}
	
	
	
	
	/*
	 * test
	 */

    public  void pingtest() throws Exception  {
       
 		
	 

		try {
				
				String abc ="";
				adminSerVice.scjul();
				
			
			} catch (Exception e) {
				// TODO: handle exception	
				e.printStackTrace();
				throw new Exception();
			}
			
	
    }
	
	
	/*
	 * 寃��湲����
	 */
	@RequestMapping(value = "/admin/upartdel", method = RequestMethod.POST)
    public  ModelAndView upArtDel(ModelMap model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
       

		try {
		
				System.out.println("sss");
				adminSerVice.upArtDel(adminDto);
			
			} catch (Exception e) {
				// TODO: handle exception
				
				
				e.printStackTrace();
				
			}
			
			WebUtil.printJsAlert(response, "��� ����듬���",request.getContextPath()+"/admin/artlist.do");
	        return null;
      
    }
	/*
	 * 怨�� 踰�� 媛���ㅺ린
	 */
	@RequestMapping(value = "/admin/boardcd", method = RequestMethod.POST)
    public  ModelAndView boardcd(Model model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
		
	  ModelAndView mav = new ModelAndView(new MappingJacksonJsonView());
	  mav.setViewName("jsonView");
	
		try {
			
		
			//List<AdminDto> list = adminSerVice.getBoardCd(adminDto.getBoard_type());
			
			List<HashMap<String, Object>> outData = new ArrayList<HashMap<String, Object>>();
			
			outData = adminSerVice.getBoardCd(adminDto.getBoard_type());
			
			List<Map> listjob=new ArrayList<Map>();
			
			Map<String, Object> detail = new HashMap<String, Object>();
			for(int i=0; i<outData.size();i++){
				   
				   detail = (Map<String, Object>)outData.get(i);
				   logger.debug(">>> board_cd..........."+detail.get("board_cd"));
				   listjob.add(detail);
			 }
			
			
			mav.addObject(listjob);
					
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return mav;
	}
	
	/*
	 *  寃����由ъ���
	 */ 
	 
	@RequestMapping(value = "/admin/artlist", method = RequestMethod.GET)
	public  ModelAndView artlist(ModelMap model ,HttpServletRequest request ,HttpServletResponse response, AdminDto adminDto) throws Exception  {
		
		ModelAndView  mv = new ModelAndView();
		try {
			
			
			PageData pd = new PageData();
			
			if(adminDto.getCpage()==null || "".equals(adminDto.getCpage())){
				
				pd.setCpage(1);			 //��㈃ ���		
				adminDto.setIntcpage(0); //荑쇰━ ���  
				
			}else{
				
				pd.setCpage(Integer.parseInt(adminDto.getCpage()));
				adminDto.setIntcpage(Integer.parseInt(adminDto.getCpage()) -1);
			}
			
			
			
			
			int index = adminDto.getIntcpage();
			if(index==0){}
			else{index*=10;}
			 
			adminDto.setIntcpage(index);
			//test

			//total
			adminDto.setMax(adminSerVice.getArtCnt(adminDto));
			
			
			pd.setTotalPage((adminDto.getMax() / 10)+1);			
			pd.setEndPage(pd.getTotalPage());
			pd.setNextPage();
			pd.setPrevPage();
			
			
			
			mv.addObject("list",adminSerVice.getArtList(adminDto));
			mv.addObject("pd",pd);
			mv.addObject("AdminDto",adminDto);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
		
		
		return mv;
	}
	

	
	
	
}
